package com.tcm.prescribe.service;


import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.core.dozer.DozerConvert;
import com.base.core.utils.ListUtils;
import com.tcm.prescribe.api.domain.dto.FrequentItemDTO;
import com.tcm.prescribe.api.domain.entity.FrequentItem;
import com.tcm.prescribe.api.domain.enums.BusinessCodeEnum;
import com.tcm.prescribe.api.service.FrequentItemService;
import com.tcm.prescribe.mapper.FrequentItemMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 6000, delay = -1)
public class FrequentItemServiceImpl implements FrequentItemService {

    @Autowired
    private FrequentItemMapper frequentItemMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", check = false, timeout = 6000, retries = 3)
    private UidGenerator uidGenerator;

    /**
     * 查询常用方药材
     *
     * @param frequentId 常用方id
     * @return
     */
    @Override
    public List<FrequentItemDTO> listItemByFrequentId(Long frequentId) throws ServiceException {

        List<FrequentItem> frequentItems = frequentItemMapper.listItemByFrequentId(frequentId);
        List<FrequentItemDTO> frequentItemDTOS = null;
        if(ListUtils.isNotEmpty(frequentItems)){
            frequentItemDTOS = DozerConvert.copyList(frequentItems, FrequentItemDTO.class);
        }
        return frequentItemDTOS;
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    @Override
    public int save(FrequentItemDTO frequentItemDto) throws ServiceException{
        if(ObjectUtil.isEmpty(frequentItemDto)){
            R.throwFail(BusinessCodeEnum.FREQUENT_DATA_NOT_EXISTS);
        }
        FrequentItem frequentItem = DozerConvert.copyProperties(frequentItemDto, FrequentItem.class);
        frequentItem.setId(uidGenerator.getUID());
        frequentItem.setCreateTime(LocalDateTime.now());
        frequentItem.setUpdateTime(LocalDateTime.now());
        frequentItem.setDeleted(new Byte("0"));
        return frequentItemMapper.insert(frequentItem);
    }

    /**
     * 更新
     *
     * @param
     * @return
     */
    @Override
    public int update( FrequentItemDTO frequentItemDto) throws ServiceException{
        if(ObjectUtil.isEmpty(frequentItemDto)){
            R.throwFail(BusinessCodeEnum.FREQUENT_DATA_NOT_EXISTS);
        }
        if(ObjectUtil.isEmpty(frequentItemDto.getId())){
            R.throwFail(BusinessCodeEnum.ID_NOT_EXISTS);
        }
        FrequentItem frequentItem = DozerConvert.copyProperties(frequentItemDto, FrequentItem.class);
        frequentItem.setDeleted(new Byte("0"));
        frequentItem.setUpdateTime(LocalDateTime.now());
        return frequentItemMapper.update(frequentItem);
    }



    /**
     * 永久删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Long id) throws ServiceException{
        return frequentItemMapper.deleteById(id);
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteLogicById(Long id) throws ServiceException{
        return frequentItemMapper.deleteLogicById(id);
    }

    @Override
    public int deleteByFrequentId(Long frequentId) throws ServiceException{
        return frequentItemMapper.deleteByFrequentId(frequentId);
    }

    @Override
    public int deleteLogicByFrequentId(Long frequentId) throws ServiceException {
        return frequentItemMapper.deleteLogicByFrequentId(frequentId);
    }

    /**
     * 查询医案信息
     *
     * @param id
     * @return
     */
    @Override
    public FrequentItemDTO findById(Long id) throws ServiceException{
        FrequentItem frequentItem = frequentItemMapper.findById(id);
        FrequentItemDTO frequentItemDTO = null;
        if(ObjectUtil.isNotEmpty(frequentItem)){
            frequentItemDTO = DozerConvert.copyProperties(frequentItem, FrequentItemDTO.class);
        }
        return frequentItemDTO;
    }
}
